import { defineStore } from 'pinia'
import axios from 'axios'
import { feedbackToast } from '@/utils/common'
import store from '../index'
import { getTMFriendID } from '@/utils/storage'

interface UserType {
  ComName: string,
  ComMain: string,
  ComInfo: string,
  UserMobilePic: string,
  VIPlog: string,
  UserVipYear: string,
  ComEwmPic: string,
  ComHtml: string
  UserMobile: string
}

interface StateType {
  userInfo: Partial<UserType>
}

const useStore = defineStore('friend', {
  state: (): StateType => ({
    userInfo: {} as UserType,
  }),
  getters: {
    storeUserInfo: (state) => state.userInfo,
  },
  actions: {
    async getUserInfoFromReq() {
      try {
        const IMFriendID = getTMFriendID()
        const { data } = await axios.get(
          `${import.meta.env.VITE_API_URL}/api/FeijiuApp/get_imshopinfo?shopuid=${IMFriendID}&strrsa=B19pHBMJrHpIhQvrhUmeSRAipcugPyXlD8vfXoind`,
        )
        this.userInfo = {
          ...data.data,
        }
      } catch (error) {
        feedbackToast({ error, message: '获取用户信息失败' })
      }
    },
  },
})

export default function useFriendStore() {
  return useStore(store)
}
